package com.yohann.algorithm;

import java.util.Scanner;

/**
 * <p>
 * 哈希表演示
 * </p>
 *
 * @author Yohann
 * @since 2021/1/2 15:53
 */
public class HashTableDemo {
    public static void main(String[] args) {
        //创建哈希表
        HashTable hashTable = new HashTable(7);

        Scanner scanner = new Scanner(System.in);
        //操作菜单
        a:
        while (true) {
            System.out.println("add:添加");
            System.out.println("display:显示");
            System.out.println("find:查找");
            System.out.println("exit:退出");

            String input = scanner.nextLine();
            switch (input) {
                case "add":
                    System.out.print("id:");
                    String id = scanner.nextLine();
                    System.out.print("name:");
                    String name = scanner.nextLine();
                    Employee employee = new Employee(Integer.valueOf(id), name);
                    hashTable.add(employee);
                    break;
                case "display":
                    hashTable.display();
                    break;
                case "find":
                    System.out.print("id:");
                    String find = scanner.nextLine();
                    hashTable.findById(Integer.valueOf(find));
                    break;
                default:
                    break a;
            }
        }
    }
}

/**
 * 模拟哈希表
 */
class HashTable {
    private EmployeeLinkedList[] linkedList;
    private int size;

    //初始化
    public HashTable(int size) {
        this.size = size;
        linkedList = new EmployeeLinkedList[size];

        for (int i = 0; i < linkedList.length; i++) {
            linkedList[i] = new EmployeeLinkedList();
        }
    }

    //添加对象
    public void add(Employee employee) {
        //根据员工id，得到添加到的链表下标
        int index = hashFun(employee.id);
        linkedList[index].add(employee);
    }

    //遍历哈希表
    public void display() {
        for (int i = 0; i < linkedList.length; i++) {
            linkedList[i].display(i);
        }
    }

    public void findById(int id) {
        int index = hashFun(id);
        Employee employee = linkedList[index].findById(id);

        if (employee == null) {
            System.out.println("未找到目标对象");
        } else {
            System.out.println(String.format("在第%s条链表中找到：id=%s，name=%s", index, id, employee.name));
        }
    }

    //散列函数
    public int hashFun(int id) {
        return id % size;
    }
}

/**
 * 实体类链表
 */
class EmployeeLinkedList {
    private Employee head;

    /**
     * 添加
     *
     * @param employee 对象
     */
    public void add(Employee employee) {
        if (head == null) {
            head = employee;
        } else {
            //插入尾部
            Employee current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = employee;
        }
    }

    /**
     * 遍历
     */
    public void display(int no) {
        if (head == null) {
            System.out.println(String.format("当前链表:%s 为空", no));
        } else {
            Employee current = head;
            System.out.println(String.format("当前链表:%s信息：", no));
            while (true) {
                System.out.println(String.format("id=%s，name=%s\t", current.id, current.name));

                if (current.next == null) {
                    break;
                }
                current = current.next;
            }
        }
    }

    /**
     * 查找
     *
     * @param id id
     * @return 对象
     */
    public Employee findById(int id) {
        if (head == null) {
            System.out.println("不存在该对象");
            return null;
        }

        Employee current = head;
        while (true) {
            if (current.id == id) {
                return current;
            }

            if (current.next == null) {
                System.out.println("不存在该对象");
                return null;
            }

            current.next = null;
        }
    }
}

/**
 * 实体类
 */
class Employee {
    public int id;
    public String name;
    public Employee next;

    public Employee(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
}